<?php
/**
 * quality.php
 *
 * @copyright 2018 OpenCart.cn
 *
 * All Rights Reserved
 * @link http://guangdawangluo.com
 *
 * @author stiffer.chen <chenlin@opencart.cn>
 * @created 2018-07-09 22:43
 * @modified 2018-07-09 22:43
 */

class ControllerExtensionModuleQuality extends Controller
{
    private $error = array();

    public function index()
    {
        $this->load->language('extension/module/quality');
        $this->document->setTitle($this->language->get('heading_title'));
        $this->load->model('setting/setting');

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
            $this->model_setting_setting->editSetting('module_quality', $this->request->post);
            $this->session->data['success'] = $this->language->get('text_success');
            $this->response->redirect($this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=module', true));
        }

        if (isset($this->error['warning'])) {
            $data['error_warning'] = $this->error['warning'];
        } else {
            $data['error_warning'] = '';
        }

        if (isset($this->error['image'])) {
            $data['error_image'] = $this->error['image'];
        } else {
            $data['error_image'] = '';
        }

        $data['action'] = $this->url->link('extension/module/quality', 'user_token=' . $this->session->data['user_token'], true);
        $data['cancel'] = $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=module', true);

        $breads = new Breads();
        $breads->add(trans('text_home'), uriLink('common/dashboard', 'user_token=' . $this->session->data['user_token']));
        $breads->add(trans('text_extension'), uriLink('marketplace/extension', 'type=module&user_token=' . $this->session->data['user_token']));
        $breads->add(trans('heading_title'), uriLink('extension/module/quality', 'user_token=' . $this->session->data['user_token']));
        $data['breadcrumbs'] = $breads->get();

        if (isset($this->request->post['module_quality_image'])) {
            $module_quality_image = $this->request->post['module_quality_image'];
        } else {
            $module_quality_image = $this->config->get('module_quality_image');
        }

        if (isset($this->request->post['module_quality_status'])) {
            $data['module_quality_status'] = $this->request->post['module_quality_status'];
        } else {
            $data['module_quality_status'] = $this->config->get('module_quality_status');
        }

        $data['thumb'] = resizeImage($module_quality_image);
        $data['module_quality_image'] = $module_quality_image;
        $data['placeholder'] = resizeImage('no_image.png');

        $data['header'] = $this->load->controller('common/header');
        $data['column_left'] = $this->load->controller('common/column_left');
        $data['footer'] = $this->load->controller('common/footer');

        $this->response->setOutput($this->load->view('extension/module/quality', $data));
    }

    protected function validate() {

        if (!$this->user->hasPermission('modify', 'extension/module/quality')) {
            $this->error['warning'] = $this->language->get('error_permission');
        }

        if (!$this->request->post['module_quality_image']) {
            $this->error['image'] = trans('error_image');
        }

        return !$this->error;
    }
}